package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.FixedAssetInventory;
import com.amsp.oa.oademo.mapper.FixedAssetInventoryMapper;
import com.amsp.oa.oademo.service.FixedAssetInventoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FixedAssetInventoryServiceImpl extends ServiceImpl<FixedAssetInventoryMapper, FixedAssetInventory>
        implements FixedAssetInventoryService {

    @Override
    public IPage<FixedAssetInventory> getPage(Integer current, Integer size, String assetCode, String assetName,
            String department, String responsiblePerson, String startDate, String endDate) {
        Page<FixedAssetInventory> page = new Page<>(current, size);

        LambdaQueryWrapper<FixedAssetInventory> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        queryWrapper.like(assetCode != null && !assetCode.isEmpty(),
                FixedAssetInventory::getAssetCode, assetCode)
                .like(assetName != null && !assetName.isEmpty(),
                        FixedAssetInventory::getAssetName, assetName)
                .like(department != null && !department.isEmpty(),
                        FixedAssetInventory::getDepartment, department)
                .like(responsiblePerson != null && !responsiblePerson.isEmpty(),
                        FixedAssetInventory::getResponsiblePerson, responsiblePerson);

        // 添加日期范围查询
        if (startDate != null && !startDate.isEmpty()) {
            queryWrapper.ge(FixedAssetInventory::getCreateTime, startDate + " 00:00:00");
        }
        if (endDate != null && !endDate.isEmpty()) {
            queryWrapper.le(FixedAssetInventory::getCreateTime, endDate + " 23:59:59");
        }

        // 按创建时间倒序排序
        queryWrapper.orderByDesc(FixedAssetInventory::getCreateTime);

        return this.page(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<FixedAssetInventory> inventoryList, String reviewer, String confirmer,
            String sampler, String rechecker, String initialChecker) {
        // 设置签字信息
        inventoryList.forEach(inventory -> {
            inventory.setReviewer(reviewer);
            inventory.setConfirmer(confirmer);
            inventory.setSampler(sampler);
            inventory.setRechecker(rechecker);
            inventory.setInitialChecker(initialChecker);
        });

        // 分离需要更新和新增的记录
        List<FixedAssetInventory> toUpdateList = inventoryList.stream()
                .filter(item -> item.getId() != null)
                .collect(Collectors.toList());

        List<FixedAssetInventory> toInsertList = inventoryList.stream()
                .filter(item -> item.getId() == null)
                .collect(Collectors.toList());

        // 执行更新操作
        if (!toUpdateList.isEmpty()) {
            this.updateBatchById(toUpdateList);
        }

        // 执行新增操作
        if (!toInsertList.isEmpty()) {
            this.saveBatch(toInsertList);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBatch(Long id, List<FixedAssetInventory> inventoryList, String reviewer, String confirmer,
            String sampler, String rechecker, String initialChecker) {
        // 设置签字信息和ID
        inventoryList.forEach(inventory -> {
            inventory.setReviewer(reviewer);
            inventory.setConfirmer(confirmer);
            inventory.setSampler(sampler);
            inventory.setRechecker(rechecker);
            inventory.setInitialChecker(initialChecker);
        });

        // 分离需要更新和新增的记录
        List<FixedAssetInventory> toUpdateList = inventoryList.stream()
                .filter(item -> item.getId() != null)
                .collect(Collectors.toList());

        List<FixedAssetInventory> toInsertList = inventoryList.stream()
                .filter(item -> item.getId() == null)
                .peek(item -> item.setId(id)) // 为新记录设置主表ID
                .collect(Collectors.toList());

        // 执行更新操作
        if (!toUpdateList.isEmpty()) {
            this.updateBatchById(toUpdateList);
        }

        // 执行新增操作
        if (!toInsertList.isEmpty()) {
            this.saveBatch(toInsertList);
        }

        return true;
    }
}